import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { WorkstationModel } from '#/api/master/models';

import { isUndefined } from '@vben/utils';

import { z } from '#/adapter/form';
import { useWorkstationTypeOptions } from '#/api/master/models';

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      dependencies: {
        triggerFields: ['wsCode'],
        disabled(values) {
          return !isUndefined(values.id);
        },
      },
      fieldName: 'wsCode',
      label: '工位代码',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'wsName',
      label: '工位名称',
      rules: 'required',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: useWorkstationTypeOptions(),
      },
      fieldName: 'wsType',
      label: '工位类型',
      rules: 'required',
      defaultValue: 1,
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 1,
        precision: 0,
      },
      fieldName: 'order',
      label: '排序',
      defaultValue: 100,
      rules: z.number().gte(0),
    },
    {
      component: 'Switch',
      componentProps: {
        checkedValue: 1,
        unCheckedValue: 0,
        checkedChildren: '启用',
        unCheckedChildren: '禁用',
        class: 'w-auto',
      },
      fieldName: 'status',
      label: '状态',
      defaultValue: 1,
      rules: 'required',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<WorkstationModel>,
): VxeTableGridOptions<WorkstationModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'wsCode', title: '工位编号', width: 140 },
    { field: 'wsName', title: '工位名称', minWidth: 180 },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: { name: 'CellTag', attrs: { useStatus: true } },
    },
    { field: 'order', title: '排序', width: 60 },
    {
      field: 'wsType',
      title: '工位类型',
      width: 140,
      cellRender: {
        name: 'CellTag',
        options: useWorkstationTypeOptions(),
      },
    },
    {
      cellRender: {
        attrs: {
          nameField: 'wsName',
          nameTitle: '工位',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          'edit', // 默认的编辑按钮
          'delete', // 默认的删除按钮
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 120,
    },
  ];
}
